package com.jwthhealth_pub.old.report.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.LogUtil;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.report.model.resbody.PhisStatDescResp;
import com.jwthhealth_pub.old.report.view.adapter.PhysStatDescAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;

/**
 * 身体状态描述
 * Created by liuzhen on 2017/10/27.
 */

public class PhysicalStatDescActivity extends BaseActivity {

    private static String LogTag = makeLogTag(PhysicalStatDescActivity.class);

    @BindView(R.id.title_layout)
    TitleLayout titleLayout;
    @BindView(R.id.rv_status)
    RecyclerView rvStatus;
    private Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phys_status_desc);
        ButterKnife.bind(this);

        mIntent = getIntent();
        getDatas();
    }

    private void getDatas() {

        final String desc = mIntent.getStringExtra("desc");
        if (desc == null) return;


//        new Thread() {
//            @Override
//            public void run() {
//                super.run();
//
//                HashMap<String, String> hashMap = new HashMap<>();
//                hashMap.put("str", desc);
//                hashMap.put("dev", ApiHelper.deviceId);
//                hashMap.put("mac", ApiHelper.macAddress);
//
//                String sign = ReqUtil.getSign(hashMap, null);
//
//                ApiHelper.request(ApiHelper.BASE_URL + "hra-new/report-str-desc?str=" + desc + "&dev=" + ApiHelper.deviceId + "&mac=" +
//                        ApiHelper.macAddress + "&sign="+sign);
//
//            }
//        }.start();


        final Call<PhisStatDescResp> _call = ApiHelper.getPhiStateDesc(desc);
        Observable<PhisStatDescResp.DataBean> observable = Observable.create(new ObservableOnSubscribe<PhisStatDescResp.DataBean>() {
            @Override
            public void subscribe(ObservableEmitter<PhisStatDescResp.DataBean> e) throws Exception {
                showLoadProgresbar();

                PhisStatDescResp body = _call.execute().body();
                if (body == null) {
                    e.onError(new Exception(getResources().getString(R.string.common_net_error)));
                    return;
                }
                String code = body.getCode();
                if (code == null) {
                    e.onError(new Exception(getResources().getString(R.string.common_net_error)));
                    return;
                }
                if (code.equals("0")) {
                    e.onNext(body.getData());
                } else {
                    e.onError(new Exception(body.getMsg()));
                }
            }
        });

        Observer<PhisStatDescResp.DataBean> observer = new Observer<PhisStatDescResp.DataBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(PhisStatDescResp.DataBean data) {
                missLoadProgressbar();
                renderingUi(data);
            }

            @Override
            public void onError(Throwable e) {
                missLoadProgressbar();
                LogUtil.toast(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        };

        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(observer);

    }

    private void renderingUi(PhisStatDescResp.DataBean data) {

        if (data == null) return;

        String title = mIntent.getStringExtra("title");
        if (title != null && data.getTitle() != null)
            titleLayout.setTitle(title + data.getTitle());

        rvStatus.setLayoutManager(new LinearLayoutManager(this));
        List<PhisStatDescResp.DataBean.SonBean> son = data.getSon();
        if (son != null && son.size() >= 0)
            rvStatus.setAdapter(new PhysStatDescAdapter(this, son));
    }

    @Override
    protected void title() {

        String title = mIntent.getStringExtra("title");
        if (title != null)
            titleLayout.setTitle(title);

        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

}
